/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package questionbook.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.model.vocabulary.RDF;
import org.openrdf.model.vocabulary.RDFS;
import questionbook.Configuration.QuestionbookConfiguration;
import questionbook.exception.IndividualAlreadyExistsException;
import questionbook.model.Answer;
import questionbook.model.Comment;
import questionbook.model.CommentType;
import questionbook.service.common.GenericServiceImpl;
import questionbook.standard.Qbook;

/**
 *
 * @author stefanstift
 */
public class CommentServiceBean extends GenericServiceImpl implements CommentService {

      private PersonService personService = new PersonServiceBean();

      public void comment(Comment comment, Answer source) {

        this.storeManager.getStore().add(comment.getUri(), RDF.TYPE, Qbook.getInstance().getCommentURI());
        this.storeManager.getStore().add(comment.getCommenter().getUri(), Qbook.getInstance().getSaysURI(), comment.getUri());
        this.storeManager.getStore().add(comment.getUri(), Qbook.getInstance().getCreatedAtURI(), comment.getCreatedAtLiteral());

        if(comment.getCommentType() == null)
            this.storeManager.getStore().add(source.getUri(), Qbook.getInstance().getCommentsURI(), comment.getUri());
        else
            this.storeManager.getStore().add(source.getUri(), comment.getCommentType().getUri(), comment.getUri());

        if(comment.getText() != null && !comment.getText().isEmpty())
            this.storeManager.getStore().add(comment.getUri(), RDFS.LABEL, comment.getTextLiteral());
    }

     public List<Comment> getByAnswer(Answer a) {

        List<Comment> commentList = new ArrayList<Comment>();

        List<Map> resultList = this.storeManager.getStore().runSPARQL("SELECT ?comment ?text ?commenter ?createdAt"
                + " WHERE { ?comment <" + RDF.TYPE + "> <" + Qbook.getInstance().getCommentURI() + "> ."
                + " ?comment <" + RDFS.LABEL + "> ?text ."
                + " ?commenter <" + Qbook.getInstance().getSaysURI() + "> ?comment ."
                + " <" + a.getUri() + "> <" + Qbook.getInstance().getCommentsURI() + "> ?comment ."
                + " ?comment <" + Qbook.getInstance().getCreatedAtURI() + "> ?createdAt "
                + "}");

        for(Map m : resultList)
        {
            Comment c = new Comment(((Value) m.get("comment")).stringValue());
            c.setText(((Value) m.get("text")).stringValue());
            c.setCreatedAt(((Value) m.get("createdAt")).stringValue());

            URI commenter = this.storeManager.getStore().getURIref(((Value) m.get("commenter")).stringValue());
            c.setCommenter(this.personService.get(commenter));

            this.loadCommentType(a, c);

            if(!commentList.contains(c))
                commentList.add(c);
        }

        return commentList;
    }

    public void loadCommentType(Answer answer, Comment comment)
    {
        List<Statement> res = this.storeManager.getStore().tuplePattern(answer.getUri(), null, comment.getUri());

        for(Statement s : res)
        {
            CommentType a =  Qbook.getInstance().getCommentType(s.getPredicate().stringValue());

            if(comment.getCommentType() != null
                    && !comment.getCommentType().getUri().stringValue().equals(Qbook.getInstance().getCommentsURI().stringValue()))
                return;

            if(a != null)
                comment.setCommentType(a);
        }
    }

    public Comment getIdentifiedComment() throws IndividualAlreadyExistsException{

        int i = 0;
        String commentnBase = "/qbComment";
        Date d = new Date();
        String timestamp = Long.toString(d.getTime());

        String id = QuestionbookConfiguration.getInstance().getBaseURL() + commentnBase + timestamp;
        URI uri = this.storeManager.getStore().getURIref(id);

        List check = this.storeManager.getStore().tuplePattern(uri, null, null);

        while(check != null || !check.isEmpty())
        {
            if(i == 10)
            {
                break;
            }

            d = new Date();
            timestamp = Long.toString(d.getTime());
            id = QuestionbookConfiguration.getInstance().getBaseURL() + commentnBase + timestamp;
            uri = this.storeManager.getStore().getURIref(id);

            check = this.storeManager.getStore().tuplePattern(uri, null, null);

            i++;
        }

        if(check == null || check.isEmpty())
        {
            Comment c = new Comment(uri);
            c.setCreatedAt(new Date());
            return c;
        }

        throw new IndividualAlreadyExistsException("Tried to create Question with URI " + id + " - but it already exists in the Store");
    }


}
